package org.geohunter.activity.osmmap;

import org.andnav.osm.util.GeoPoint;
import org.andnav.osm.views.OpenStreetMapView;
import org.andnav.osm.views.overlay.OpenStreetMapViewOverlay;
import org.andnav.osm.views.util.OpenStreetMapRendererInfo;
import org.geohunter.activity.map.Area;
import org.geohunter.activity.map.IMapView;
import org.geohunter.activity.map.OverlayManager;

import android.content.Context;
import android.util.Log;

public class OsmMapView extends OpenStreetMapView 
implements IMapView<OpenStreetMapViewOverlay> {
    private OverlayManager<OpenStreetMapViewOverlay> mOverlayManager;
    private final int mSwitchableOverlayIndex;
    private final OpenStreetMapViewOverlay mEmptyOverlay;

    public OsmMapView(Context context, OpenStreetMapRendererInfo rendererInfo,
            OpenStreetMapViewOverlay myLocationOverlay,
            OpenStreetMapViewOverlay emptyOverlay) {
        super(context, rendererInfo);
        mOverlayManager = null;
        mEmptyOverlay = emptyOverlay;
        
        mSwitchableOverlayIndex = getOverlays().size();
        Log.d("geohunter", "mSwitchableOverlayIndex = " + mSwitchableOverlayIndex);
        getOverlays().add(emptyOverlay);
        getOverlays().add(myLocationOverlay);
    }

    public Area getVisibleArea() {
        OpenStreetMapViewProjection projection = super.getProjection();
        if (projection == null)  //Not inited yet
            return new Area(0, 0, 1, 1);  //dummy values for now
        GeoPoint newTopLeft = projection.fromPixels(0, 0);
        GeoPoint newBottomRight = projection.fromPixels(super.getRight(), 
                super.getBottom());
        Area area = new Area();
        area.mLatLow = newBottomRight.getLatitudeE6() / 1.0E6;
        area.mLatHigh = newTopLeft.getLatitudeE6() / 1.0E6;
        area.mLonLow = newTopLeft.getLongitudeE6() / 1.0E6;
        area.mLonHigh = newBottomRight.getLongitudeE6() / 1.0E6;
        //Log.d("geohunter", "getVisibleArea " + area.mLatLow + "," + area.mLonLow + " - " +
        //        area.mLatHigh + "," + area.mLonHigh);
        return area;
    }

    //TODO: Capture screen orientation changes
    
    public void setOverlayManager(OverlayManager<OpenStreetMapViewOverlay> overlayManager) {
        mOverlayManager = overlayManager;
    }
    
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        //Log.d("geohunter", "onScrollChanged");
        mOverlayManager.onScrollTo(getVisibleArea());
    }

    @Override
    public void clearOverlay() {
        getOverlays().set(mSwitchableOverlayIndex, mEmptyOverlay);
        postInvalidate();
    }

    @Override
    public void setOverlay(OpenStreetMapViewOverlay overlay) {
        getOverlays().set(mSwitchableOverlayIndex, overlay);
        postInvalidate();
    }

    public void updateIfMoved() {
        //Always updates for now
        Area area = getVisibleArea();
        mOverlayManager.onScrollTo(area);
    }
}
